﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace iToolPlugin.SMSProvider
{
    public static class SmsSenderUtil
    {
        public static long getCurrentTime()
        {
            return (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
        }

        public static long getRandom()
        {
            return (long)new Random((int)getCurrentTime()).Next() % 900000L + 100000;
        }

        public static string calculateSignature(string appkey, long random, long time, string phoneNumber)
        {
            StringBuilder stringBuilder = new StringBuilder("appkey=").Append(appkey).Append("&random=").Append(random)
                .Append("&time=")
                .Append(time)
                .Append("&mobile=")
                .Append(phoneNumber);
            return sha256(stringBuilder.ToString());
        }

        public static string calculateSignature(string appkey, long random, long time, string[] phoneNumbers)
        {
            StringBuilder stringBuilder = new StringBuilder("appkey=").Append(appkey).Append("&random=").Append(random)
                .Append("&time=")
                .Append(time)
                .Append("&mobile=");
            if (phoneNumbers.Length != 0)
            {
                stringBuilder.Append(phoneNumbers[0]);
                for (int i = 1; i < phoneNumbers.Length; i++)
                {
                    stringBuilder.Append(",");
                    stringBuilder.Append(phoneNumbers[i]);
                }
            }

            return sha256(stringBuilder.ToString());
        }

        public static string calculateSignature(string appkey, long random, long time)
        {
            StringBuilder stringBuilder = new StringBuilder("appkey=").Append(appkey).Append("&random=").Append(random)
                .Append("&time=")
                .Append(time);
            return sha256(stringBuilder.ToString());
        }

        public static string calculateAuth(string appkey, long random, long time, string fileSha1Sum)
        {
            StringBuilder stringBuilder = new StringBuilder("appkey=").Append(appkey).Append("&random=").Append(random)
                .Append("&time=")
                .Append(time)
                .Append("&content-sha1=")
                .Append(fileSha1Sum);
            return sha256(stringBuilder.ToString());
        }

        public static string sha1sum(byte[] bytes)
        {
            byte[] array = SHA1.Create().ComputeHash(bytes);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < array.Length; i++)
            {
                stringBuilder.Append(array[i].ToString("X2"));
            }

            return stringBuilder.ToString().ToLower();
        }

        private static string sha256(string rawString)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(rawString);
            byte[] array = SHA256.Create().ComputeHash(bytes);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < array.Length; i++)
            {
                stringBuilder.Append(array[i].ToString("X2"));
            }

            return stringBuilder.ToString().ToLower();
        }
    }
}
